<?php

namespace ZfEvent\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\EventManager\EventManager;

/*-------------------------------import_Acl---------------------------------*/
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Role\GenericRole as Role;
use Zend\Permissions\Acl\Resource\GenericResource as Resource;
/*-------------------------------end_import_Acl---------------------------------*/

class IndexController extends AbstractActionController
{

    public function indexAction()
    {
        /*$events = new EventManager();
        $events->attach('do' , function($e){
            $event = $e->getName();
            $params = $e->getParams();
            printf(
                'Handled event "%s", with parameters %s',
                $event,
                json_encode($params)
            );
        });

        $params = array('foo' => 'bar' , 'baz' => 'bat');
        $events->trigger('foo' , null , $params);*/
        $this->getEventManager()->trigger('MyEvent', $this);
        return new ViewModel();
    }

    public function aclAction()
    {
        /*
        $acl = new Acl();

        $acl->addRole(new Role('guest'))
            ->addRole(new Role('member'))
            ->addRole(new Role('admin'));

        $parents = array('guest', 'member', 'admin');
        $acl->addRole(new Role('someUser'), $parents);

        $acl->addResource(new Resource('someResource'));

        $acl->deny('guest', 'someResource');
        $acl->allow('member', 'someResource');
        $acl->deny('someUser' , 'someResource');
        echo $acl->isAllowed('someUser', 'someResource') ? 'allowed' : 'denied';
        echo $acl->isAllowed('member' , 'someResource') ? 'allowed' : 'denied';*/

        $acl = new Acl();
        $roleGuest = new Role('guest');
        $acl->addRole($roleGuest);


        $acl->addRole(new Role('staff') , $roleGuest);

        $acl->addRole(new Role('editor') , 'staff');

        // Administrator does not inherit access controls
        $acl->addRole(new Role('administrator'));

        //guest may only view content

        $acl->allow($roleGuest , null , 'view');

        $acl->allow('staff' , null , array('edit' , 'submit' , 'revise'));

        $acl->allow('editor' , null , array('publish', 'archive', 'delete'));

        $acl->allow('administrator');


        echo $acl->isAllowed('guest' , null , 'view') ? 'allow' : 'denied';

        echo $acl->isAllowed('staff', null, 'publish') ?
            "allowed" : "denied";
// denied

        echo $acl->isAllowed('staff', null, 'revise') ?
            "allowed" : "denied";
// allowed

        echo $acl->isAllowed('editor', null, 'view') ?
            "allowed" : "denied";
// allowed because of inheritance from guest

        echo $acl->isAllowed('editor', null, 'update') ?
            "allowed" : "denied";
// denied because no allow rule for 'update'

        echo $acl->isAllowed('administrator', null, 'view') ?
            "allowed" : "denied";
// allowed because administrator is allowed all privileges

        echo $acl->isAllowed('administrator') ?
            "allowed" : "denied";
// allowed because administrator is allowed all privileges

        echo $acl->isAllowed('administrator', null, 'update') ?
            "allowed" : "denied";
// allowed because administrator is allowed all privileges

    }

}

